load("//bazel:api.bzl", "lit_tests", "mojo_filecheck_test", "mojo_test")

_FILECHECK_TESTS = [
    "test_exit_1.mojo",
]

_LIT_TESTS = [
    "test_invalid_paramenv.mojo",
    "test_compile_sanitize_address.mojo",
]

_PLATFORM_CONSTRAINTS = {
    "test_aarch64_target.mojo": ["@platforms//os:macos"],  # TODO (17471): Not all aarch64 have neon, so we need to guard against that, for now just require Apple Silicon.
    "test_linux_target.mojo": ["@platforms//os:linux"],
    "test_macos_target.mojo": ["@platforms//os:macos"],
    # TODO(MSTDL-1965): Fix this ASAN issue.
    "test_compile_debug.mojo": select({
        "//:asan": ["@platforms//:incompatible"],
        "//conditions:default": [],
    }),
}

_MOJO_COMPILE_OPTS = {
    "test_compile.mojo": [
        "--debug-level",
        "none",
    ],
    "test_build_info_debug.mojo": [
        "-D",
        "BUILD_TYPE=debug",
    ],
    "test_paramenv.mojo": [
        "-D",
        "bar=99",
        "-D",
        "baz=hello",
        "-D",
        "foo=11",
        "-D",
        "my_true=True",
        "-D",
        "my_false=false",
        "-D",
        "my_on=on",
        "-D",
        "my_off=off",
    ],
    "test_compile_debug.mojo": [
        "-O0",
        "--debug-level",
        "none",
    ],
}

_EXPECT_FAIL = [
    "test_exit_1.mojo",
]

[
    mojo_filecheck_test(
        name = src + ".test",
        srcs = [src],
        copts = [
            "--debug-level",
            "full",
        ] + _MOJO_COMPILE_OPTS.get(src, []),
        expect_fail = src in _EXPECT_FAIL,
        target_compatible_with = _PLATFORM_CONSTRAINTS.get(src, []),
        deps = [
            "@mojo//:stdlib",
            "@mojo//:test_utils",
        ],
    )
    for src in _FILECHECK_TESTS
]

[
    mojo_test(
        name = src + ".test",
        srcs = [src],
        copts = [
            "--debug-level",
            "full",
        ] + _MOJO_COMPILE_OPTS.get(src, []),
        target_compatible_with = _PLATFORM_CONSTRAINTS.get(src, []),
        deps = [
            "@mojo//:stdlib",
            "@mojo//:test_utils",
        ],
    )
    for src in glob(
        ["*.mojo"],
        exclude = _FILECHECK_TESTS + _LIT_TESTS,
    )
]

lit_tests(
    name = "lit_tests",
    size = "large",
    srcs = _LIT_TESTS,
    mojo_deps = [
        "@mojo//:stdlib",
    ],
    target_compatible_with = select({
        "//:asan": ["@platforms//:incompatible"],
        "//:tsan": ["@platforms//:incompatible"],
        "//conditions:default": [],
    }),
)
